import { MyModel } from "../../models/my.js"
const myModel = new MyModel()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    authorize:false,
    userInfo:null,
    count:0,
    getMyBooks:[]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(this.data.getMyBooks)
    // 调用授权
    this.userAuthorized()
    myModel.getMyBooksCount()
      .then(res => {
        this.setData({
          myBooksCount: res.count
        })
        // console.log(res)
    })
    myModel.getMyBooks()
    .then(res => {
      this.setData({
        getMyBooks: res
      })
      // console.log(res)
    })
  },
  // 获取用户是否已经授权
  userAuthorized(){
    wx.getSetting({
      success:data=>{
        if(data.authSetting['scope.userInfo']){
          wx.getUserInfo({
            success:data=>{
              this.setData({
                authorize:true,
                userInfo:data.userInfo
              })
              // console.log(data)
            }
          })
        }else{
          console.log("获取头像失败")
        }
      }
    })
  },
  
  onGetUserInfo(event){
    const userInfo = event.detail.userInfo
    if(userInfo){
      this.setData({
        userInfo,
        authorize: true
      })
      // console.log(event)
    }
    
  },
  onJumpToAbout(event){
    wx.navigateTo({
      url: '/pages/about/about'
    })
  },
  onStudy(event){
    wx.navigateTo({
      url: '/pages/course/course'
    })
  },
  onPreviewTap: function (event) {
    wx.navigateTo({
      url: '/pages/my-detail/index?cid=' + event.detail.cid + '&type=' + event.detail.type
    })
  },
})

